home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / td.arc / TD2.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1985-07-03  |  17.9 KB  |  742 lines

  1. program td;   { version 2.12  Copright (c) 1985 by Mark Johnson 05/28/85 }
  2.  
  3.    { This program is protected under Copyright law.  It has been placed }
  4.    { in the public domain for personal non-commercial use only.  You    }
  5.    { may use this code, modify it, or give it away.  The author has     }
  6.    { relinquished personal gain from this program and so should you.    }
  7.    { This program was originally sold as a DEMO version.  The only      }
  8.    { documentation available is in the code.  If you are interested in  }
  9.    { more powerful versions for Pascal, COBOL, BASIC, PL/I, NEAT/3, or  }
  10.    { 8086 Assembler, Please contact the author.                         }
  11.    { This program is available written in NCR-COBOL, NCR ITX-Pascal and }
  12.    { IBM-PL/I for direct use on mainframes and minis.                   }
  13.  
  14.    { This program was originally written in PL/I to generate PL/I code, }
  15.    { then run through a PL/I to Pascal translator.  The output of the   }
  16.    { translator was cleaned up by hand.  Some months later when Turbo   }
  17.    { Pascal was released, a new version of this program was produced to }
  18.    { generate Pascal code.                                              }
  19.  
  20.    { Mark E. Johnson                 2272-F Benson Avenue               }
  21.    {                                 St. Paul Minnesota  55116          }
  22.    { evening phone                   612-698-3686                       }
  23.  
  24. const
  25.   debug = false;
  26.  
  27. type
  28.   ltype = string[85];
  29.   stype = string[10];
  30.  
  31. var   { this could have been a RECORD, but the PL/I to Pascal translator }
  32.       { is a bit stupid.                                                 }
  33.  
  34.     rtype     : array[1..64] of integer;
  35.     rname     : array[1..64] of ltype;
  36.     rx        : array[1..64] of integer;
  37.     ry        : array[1..64] of integer;
  38.     rlen      : array[1..64] of integer;
  39.     rscale    : array[1..64] of integer;
  40.     rorder    : array[1..64] of integer;
  41.  
  42.  
  43.     ndx       : integer;
  44.     line      : ltype;
  45.     lineno    : integer;
  46.     colno     : integer;
  47.     token     : ltype;
  48.     tail      : string[32];
  49.     i,j,l     : integer;
  50.     incr      : integer;
  51.     outtype   : char;
  52.     ans       : char;
  53.     infile    : text;
  54.     outfile   : text;
  55.     libfile   : text;
  56.     procname  : string[32];
  57.     varfl     : boolean;
  58.     librfl    : boolean;
  59.     subrfl    : boolean;
  60.     ctemp     : stype;
  61.     efile     : boolean;
  62.     level     : integer;
  63.     inname    : string[15];
  64.     outname   : string[15];
  65.     libname   : string[15];
  66.  
  67. label
  68.     generate, retry, endinp;
  69.  
  70. function toupper(mess : ltype) : ltype;
  71. var
  72.   temp : ltype;
  73.   i    : integer;
  74.  
  75. begin
  76.   temp:='';
  77.   for i:=1 to length(mess) do
  78.     temp:=concat(temp,upcase(copy(mess,i,1)));
  79.   toupper:=temp;
  80. end;
  81.  
  82. procedure space(n : integer);
  83. var
  84.   i  : integer;
  85.  
  86. begin
  87.     writeln;
  88.     for i:=1 to n do
  89.       write(' ');
  90. end;
  91.  
  92. procedure enter(mess : ltype);   { ENTER and LEAVE are debugging routines }
  93. begin                            { no longer used in this program.        }
  94.   if debug = true then
  95.     begin
  96.     level:=level+1;
  97.     space(level);
  98.     write(' Entering - ',mess);
  99.   end;
  100. end;
  101.  
  102. procedure leave(mess : ltype);
  103. begin
  104.   if debug = true then
  105.     begin
  106.     level:=level-1;
  107.     space(level);
  108.     write(' Leaving - ',mess)
  109.   end;
  110. end;
  111.  
  112. function convert(num : integer) : stype;
  113.  
  114. var
  115.   st1  : stype;
  116.  
  117. begin
  118.   str(num,st1);
  119.   while copy(st1,1,1) = ' ' do
  120.     st1:=copy(st1,2,length(st1)-1);
  121.   convert:=st1;
  122. end;
  123.  
  124. function rev(f : boolean) : boolean;   { tacky }
  125. begin
  126.   if f = true
  127.     then rev:=false
  128.   else
  129.     rev:=true;
  130. end;
  131.  
  132. procedure setup;
  133. var
  134.   ans    : char;
  135.   iotype : string[8];
  136.   ftype  : char;
  137.  
  138. begin
  139.   for i:=1 to ndx-1 do
  140.     begin
  141.     if rtype[i] > 0 then
  142.       begin
  143.       clrscr;
  144.       iotype:='Out Alfa 1';
  145.       if rtype[i] = 2 then
  146.         iotype:='In Alfa 2'
  147.       else if rtype[i] = 3 then
  148.         iotype:='In Num 3';
  149.  
  150.       gotoxy(23,2);
  151.       write('Variable Definitions');
  152.       gotoxy(20,5);
  153.       write('NAME   - ');
  154.       lowvideo;
  155.       write(rname[i]);
  156.       highvideo;
  157.       gotoxy(20,7);
  158.       write('TYPE   - ');
  159.       lowvideo;
  160.       write(iotype);
  161.       highvideo;
  162.       gotoxy(20,8);
  163.       write('LENGTH - ');
  164.       lowvideo;
  165.       write(rlen[i]);
  166.       highvideo;
  167.       gotoxy(20,10);
  168.       write('SCALE  - ');
  169.       lowvideo;
  170.       write(rscale[i]);
  171.       highvideo;
  172.       gotoxy(10,20);
  173.       write('Change or add to this record?    ');
  174.       gotoxy(1,21);
  175.       read(kbd,ans);
  176.       if (ans='y') or (ans='Y') then
  177.         begin
  178.         if rtype[i]=2 then
  179.           begin
  180.           gotoxy(10,20);
  181.           write('N)umeric or A)lpha (N or A)     ');
  182.           gotoxy(40,7);
  183.           read(kbd,ans);
  184.           if (ans='n') or (ans='N') then
  185.             rtype[i]:=3;
  186.         end;
  187.         gotoxy(10,20);
  188.         write('Enter length ( 1 - 80 )         ');
  189.         gotoxy(40,8);
  190.         readln(rlen[i]);
  191.         if (rtype[i]=3) or (rtype[i]=1) then
  192.           begin
  193.           gotoxy(10,20);
  194.           write('Enter Scale (0 - 15)         ');
  195.           gotoxy(40,10);
  196.           readln(rscale[i]);
  197.         end;
  198.         { i:=i-1; }
  199.       end;
  200.     end;
  201.   end;
  202. end;
  203.  
  204. function getvar(line : ltype) : ltype;
  205. var
  206.   k     : integer;
  207.  
  208. begin
  209.   incr:=0;
  210.   if (copy(line,1,1)='!') or (copy(line,1,1)='#') then
  211.     begin
  212.     k:=pos(' ',line);
  213.     if k = 0 then
  214.       getvar:=line
  215.     else
  216.       begin
  217.       incr:=k-1;
  218.       getvar:=(copy(line,1,k-1))
  219.     end;
  220.   end
  221.   else
  222.     begin
  223.     k:=pos('!',line);
  224.     if k=0 then
  225.       k:=pos('#',line);
  226.     if k=0 then
  227.       getvar:=line
  228.     else
  229.       begin
  230.       incr:=k-1;
  231.       getvar:=copy(line,1,k-1);
  232.     end;
  233.   end;
  234. end;
  235.  
  236. function deblank(str1 : stype) : stype;
  237. var
  238.   str2      : stype;
  239.   c         : char;
  240.   i         : integer;
  241.  
  242. label 99;
  243.  
  244. begin
  245.   enter('Function deblank');
  246.   str2:=str1;
  247.   if (copy(str2,1,1)='!') or (copy(str2,1,1)='#') then
  248.     str2:=copy(str2,2,(length(str2)-1)+1);
  249.   for i:=length(str2) downto 1 do
  250.     begin
  251.     if copy(str2,i,1) <> ' ' then
  252.       goto 99;
  253.   end;
  254. 99:
  255.   deblank:=copy(str2,1,i);
  256. end;
  257.  
  258. function verify(st2 : ltype) : integer;  { return pos of 1st non-space }
  259. var
  260.   i    : integer;
  261. label gotit;
  262.  
  263. begin
  264.   for i:=1 to length(st2) do
  265.    if copy(st2,i,1) <> ' ' then
  266.      goto gotit;
  267.  
  268. gotit:
  269.   if i=length(st2) then  { all spaces }
  270.     verify:=0
  271.   else
  272.     verify:=i;
  273. end;
  274.  
  275.  
  276. Procedure menu;
  277. var
  278.   continue : boolean;
  279.  
  280. Begin
  281.  
  282.   Clrscr;
  283.   Gotoxy(11,1);
  284.   Write('Copyright (c) 1985  Mark E.Johnson - MicroTools Co.');
  285.   Gotoxy(1,2);
  286.   Write(' ');
  287.   Gotoxy(25,6);
  288.   Write('TurboDraw 2.0');
  289.   Gotoxy(27,7);
  290.   Write('File Menu');
  291.   continue:=true;
  292.   while continue = true do
  293.     begin
  294.     Gotoxy(16,9);
  295.     Write('1). Screen Input File   ');
  296.     lowvideo;
  297.     Gotoxy(40,9);
  298.     Write(inname);
  299.     highvideo;
  300.     Gotoxy(16,10);
  301.     Write('2). Pascal Output File  ');
  302.     lowvideo;
  303.     Gotoxy(40,10);
  304.     Write(outname);
  305.     highvideo;
  306.     Gotoxy(16,11);
  307.     Write('3). Library Input File  ');
  308.     lowvideo;
  309.     Gotoxy(40,11);
  310.     Write(libname);
  311.     highvideo;
  312.     gotoxy(16,12);
  313.     write('4). Exit to main menu ');
  314.     Gotoxy(16,14);
  315.     Write('Enter Option 1,2,3, or 4  ');
  316.     Gotoxy(42,14);
  317.     read(kbd,ans);
  318.     if ans='4' then
  319.       continue:=false
  320.     else
  321.       begin
  322.       Gotoxy(16,14);
  323.       Write('Enter File name or <C/R>   ')
  324.     end;
  325.     if ans='1' then
  326.       begin
  327.       lowvideo;
  328.       gotoxy(40,9);
  329.       write('               ');
  330.       gotoxy(40,9);
  331.       readln(inname);
  332.       highvideo;
  333.       inname:=toupper(inname);
  334.     end
  335.     else if ans='2' then
  336.       begin
  337.       lowvideo;
  338.       gotoxy(40,10);
  339.       write('               ');
  340.       gotoxy(40,10);
  341.       readln(outname);
  342.       highvideo;
  343.       outname:=toupper(outname)
  344.     end
  345.     else if ans='3' then
  346.       begin
  347.       lowvideo;
  348.       gotoxy(40,11);
  349.       write('               ');
  350.       gotoxy(40,11);
  351.       readln(libname);
  352.       highvideo;
  353.       libname:=toupper(libname)
  354.     end;
  355.   end;
  356. End;
  357.  
  358. procedure wrname(i : integer);
  359. var
  360.   x : integer;
  361. begin
  362.   for x:=1 to 20 do
  363.     if x <= length(rname[i]) then
  364.       write(copy(rname[i],x,1));
  365. end;
  366.  
  367. procedure sort;
  368. var
  369.     htype     : integer;
  370.     hname     : ltype;
  371.     hx        : integer;
  372.     hy        : integer;
  373.     hlen      : integer;
  374.     hscale    : integer;
  375.     horder    : integer;
  376.  
  377.     litvar,iotype,ftype : stype;
  378.     junk      : char;
  379.     ord1,ord2 : integer;
  380.     i,j       : integer;
  381.     again,l1  : boolean;
  382.  
  383. label ordl,endsort;
  384.  
  385. begin
  386.     while true do
  387.         begin
  388.         clrscr;
  389.         lowvideo;
  390.         write('Order Field Name               Literal/Variable  Input/Output  Alpha/Numeric');
  391.         highvideo;
  392.  
  393.         j:=1;
  394.         for i:=1 to ndx-1 do
  395.             begin
  396.             if j > 18 then
  397.                 begin
  398.                 j:=1;
  399.                 gotoxy(1,22);
  400.                 write('Press a key to continue ');
  401.                 read(kbd,junk);
  402.                 clrscr;
  403.                 lowvideo;
  404.                 writeln('Order Field Name               Literal/Variable  Input/Output  Alpha/Numeric');
  405.                 highvideo;
  406.  
  407.             end;
  408.             litvar:='Variable';
  409.             iotype:='Output';
  410.             ftype:='Alpha';
  411.             if rtype[i]=0 then
  412.                 litvar:='Literal'
  413.             else if rtype[i]=2 then
  414.                 iotype:='Input'
  415.             else if rtype[i]=3 then
  416.                 begin
  417.                 iotype:='Input';
  418.                 ftype:='Numeric'
  419.             end;
  420.             if rname[i] <> '' then
  421.                 begin
  422.                 gotoxy(1,j+1);
  423.                 write(rorder[i]:3);
  424.                 gotoxy(7,j+1);
  425.                 wrname(i);
  426.                 gotoxy(32,j+1);
  427.                 write(litvar);
  428.                 gotoxy(50,j+1);
  429.                 write(iotype);
  430.                 gotoxy(64,j+1);
  431.                 write(ftype);
  432.                 j:=j+1;
  433.             end;
  434.         end;
  435.         L1:=TRUE;
  436.         repeat
  437.             gotoxy(1,22);
  438.             write('Enter field to change, or 999 to quit    ');
  439.             lowvideo;
  440.             gotoxy(1,23);
  441.             write('      ');
  442.             gotoxy(1,23);
  443.             readln(ord1);
  444.             highvideo;
  445.             if ord1=999 then
  446.                 goto endsort;
  447.             for j:=1 to ndx-1 do
  448.                 if ord1=rorder[j] then
  449.                     goto ordl;
  450.  ordl:      if ord1 = rorder[j] then
  451.                 l1:=FALSE;
  452.         until l1 = false;
  453.         ord1:=j;
  454.         gotoxy(1,22);
  455.         write('Place at field #                        ');
  456.         lowvideo;
  457.         gotoxy(1,23);
  458.         write('     ');
  459.         gotoxy(1,23);
  460.         readln(ord2);
  461.         highvideo;
  462.         rorder[ord1]:=ord2;
  463.  
  464.  { Simple bubble sort is fast enough for this application }
  465.  
  466.         Again:=TRUE;
  467.         while again = true do
  468.             begin
  469.             Again:=FALSE;
  470.             for i:=1 to ndx-2 do
  471.                 begin
  472.                 If rorder[i] > rorder[i+1] Then
  473.                     begin
  474.                     hname:=rname[i];
  475.                     htype:=rtype[i];
  476.                     hx:=rx[i];
  477.                     hy:=ry[i];
  478.                     hlen:=rlen[i];
  479.                     hscale:=rscale[i];
  480.                     horder:=rorder[i];
  481.                     rname[i]:=rname[i+1];
  482.                     rtype[i]:=rtype[i+1];
  483.                     rx[i]:=rx[i+1];
  484.                     ry[i]:=ry[i+1];
  485.                     rlen[i]:=rlen[i+1];
  486.                     rscale[i]:=rlen[i+1];
  487.                     rorder[i]:=rorder[i+1];
  488.                     rname[i+1]:=hname;
  489.                     rtype[i+1]:=htype;
  490.                     rx[i+1]:=hx;
  491.                     ry[i+1]:=hy;
  492.                     rlen[i+1]:=hlen;
  493.                     rscale[i+1]:=hscale;
  494.                     rorder[i+1]:=horder;
  495.                     again:=TRUE;
  496.                 end;  { if rorder[i] }
  497.             end;      { for i:=1 to  }
  498.         end;          { while again  }
  499.      end;
  500.     endsort:
  501.  End;
  502.  
  503.  
  504. begin { main }
  505.   inname:='DEMO.SCR';
  506.   outname:='DEMO.PAS ';
  507.   libname:='TD.LIB';
  508. retry:
  509.   menu;
  510.   level:=0;
  511.   varfl:=true;
  512.   librfl:=false;
  513.   subrfl:=false;
  514.   outtype:='C';
  515.   ndx:=1;
  516.   lineno:=1;
  517.   assign(infile,inname);
  518.   {$I-}
  519.   reset(infile);
  520.   {$I+}
  521.   if ioresult <> 0 then
  522.     begin
  523.     writeln;
  524.     writeln('Screen file not found, Press a key to continue ');
  525.     read(kbd,ans);
  526.     goto retry
  527.   end;
  528.   if librfl=true then
  529.     begin
  530.     assign(libfile,'TD.LIB');
  531.     {$I-}
  532.     reset(libfile);
  533.     {$I+}
  534.     if ioresult <> 0 then
  535.       begin
  536.       writeln('LIB file not found, Press a key to continue ');
  537.       read(kbd,ans);
  538.       close(infile);
  539.       goto retry
  540.     end;
  541.   end;
  542.  
  543.   assign(outfile,outname);
  544.   rewrite(outfile);
  545.  
  546.   efile:=false;
  547.   while efile = false do
  548.     begin
  549.     colno:=1;
  550.     readln(infile,line);
  551.     if eof(infile) then
  552.       efile:=true;
  553.     l:=length(line);
  554.     i:=0;
  555.     while colno < l do
  556.       begin
  557.       i:=verify(line);
  558.       if (i=0) and (length(line) > 0) then
  559.         i:=1;
  560.       if i > 0 then
  561.         begin
  562.         colno:=colno+i+incr-1;
  563.         token:=GETVAR(copy(line,i,(length(line)-i)+1));
  564.         j:=i+length(token);
  565.         rtype[ndx]:=0;
  566.         if copy(token,1,1) = '!' then
  567.           begin
  568.           rtype[ndx]:=1;
  569.           token:=copy(token,2,length(token)-1);
  570.         end
  571.         else if copy(token,1,1) = '#' then
  572.           begin
  573.           rtype[ndx]:=2;
  574.           token:=copy(token,2,length(token)-1);
  575.         end;
  576.         rname[ndx]:= token;  {deblank(token);}
  577.         rx[ndx]:=lineno;
  578.         ry[ndx]:=colno;
  579.         rlen[ndx]:=0;
  580.         rscale[ndx]:=0;
  581.         rorder[ndx]:=ndx*10;
  582.         if j >= length(line) then
  583.           l:=0
  584.         else
  585.           line:=copy(line,j,(length(line)-j)+1);
  586.         ndx:=ndx+1;
  587.       end;
  588.     end;
  589.     lineno:=lineno+1;
  590.   end;
  591.  
  592. endinp:
  593.   close(infile);
  594.   while true do
  595.     begin
  596.     clrscr;
  597.     gotoxy(28,3);
  598.     write('TurboDraw');
  599.     gotoxy(28,6);
  600.     write('OPTIONS');
  601.     lowvideo;
  602.     gotoxy(19,10);
  603.     write('G - Generate code and exit');
  604.     gotoxy(19,11);
  605.     write('V - Variable declarations');
  606.     gotoxy(19,12);
  607.     write('O - Order of input/output');
  608.     gotoxy(19,13);
  609.     write('L - Include Library functions');
  610.     highvideo;
  611.     gotoxy(50,13);
  612.     if librfl = true then
  613.       write('Yes')
  614.     else
  615.       write(' No');
  616.     lowvideo;
  617.     gotoxy(19,14);
  618.     write('P - Generate a procedure');
  619.     highvideo;
  620.     gotoxy(50,14);
  621.     if subrfl = true then write('Yes')
  622.       else write(' No');
  623.     lowvideo;
  624.     gotoxy(19,15);
  625.     write('I - Include VAR Definitions');
  626.     highvideo;
  627.     gotoxy(50,15);
  628.     if varfl = true then write('Yes')
  629.       else write(' No');
  630.     gotoxy(19,18);
  631.     write('Enter Option: ');
  632.     read(kbd,ans);
  633.     case ans of
  634.       'p','P'  :    begin
  635.                       subrfl:=rev(subrfl);
  636.                       if subrfl=true then
  637.                         begin
  638.                         gotoxy(19,22);
  639.                         lowvideo;
  640.                         write('Enter name of procedure ');
  641.                         highvideo;
  642.                         readln(procname)
  643.                       end
  644.                     end;
  645.  
  646.        'l','L'  :  librfl:=rev(librfl);
  647.        'i','I'  :  varfl:=rev(varfl);
  648.        'g','G'  :  goto Generate;
  649.        'v','V'  :  Setup;
  650.        'o','O'  :  sort;
  651.  
  652.   end;
  653. end;
  654.  
  655.        { Generate Code for TURBO PASCAL }
  656.  
  657. generate:
  658. writeln(outfile);
  659. writeln(outfile,'{ Start of Turbodraw code }');
  660.   if varfl = true then
  661.     begin
  662.     writeln(outfile,'Var');
  663.     for i:=1 to ndx-1 do
  664.       begin
  665.       if rtype[i] > 0 then
  666.         begin
  667.         writeln(outfile);
  668.         write(outfile,'  ',rname[i]);
  669.         if rtype[i] = 1 then
  670.           write(outfile,' : Integer;')
  671.         else if rtype[i] = 2 then
  672.           write(outfile,' : String[',convert(rlen[i]),'];')
  673.         else
  674.           begin
  675.           if rscale[i] > 0 then
  676.             write(outfile,' : Real;')
  677.           else
  678.             write(outfile,' : Integer;');
  679.         end;
  680.       end;
  681.     end;
  682.     writeln(outfile);
  683.   end;
  684.   writeln(outfile);
  685.   if librfl = true then
  686.     begin
  687.     assign(libfile,libname);
  688.     reset(libfile);
  689.     while not eof(libfile) do     { Include library code }
  690.       begin
  691.       readln(libfile,line);
  692.       writeln(outfile,line);
  693.     end;
  694.   close(libfile)
  695.   end;
  696.   if subrfl = true then
  697.     begin
  698.     writeln(outfile);
  699.     writeln(outfile,'Procedure ',procname,';');
  700.     writeln(outfile,'Begin');
  701.     writeln(outfile,'  Clrscr;');
  702.   end;
  703.   for i:=1 to ndx-1 do
  704.     begin
  705.     if rname[i] > ' ' then
  706.       writeln(outfile,'  Gotoxy(',convert(ry[i]),',',convert(rx[i]),');');
  707.     if rtype[i]=0 then
  708.       begin
  709.       if rname[i] > ' ' then
  710.         writeln(outfile,'  Write(''',rname[i],''');');
  711.     end
  712.     else if rtype[i]=1 then
  713.       begin
  714.       tail:=convert(rlen[i]);
  715.       tail:=concat(':',tail);
  716.       if rscale[i] > 0 then
  717.         tail:=concat(tail,':',convert(rscale[i]));
  718.       tail:=concat(tail,');');
  719.       if rlen[i] = 0 then
  720.         writeln(outfile,'  Write(',rname[i],');')
  721.       else
  722.         writeln(outfile,'  Write(',rname[i],tail)
  723.     end
  724.  
  725.     else if (rtype[i]=2) or (rtype[i]=3) then
  726.       begin
  727.       if rlen[i] = 0 then
  728.         writeln(outfile,'  Readln(',rname[i],');')
  729.       else
  730.       if rscale[i] > 0 then
  731.         writeln(outfile,'  ',rname[i],':=Getreal(',convert(rlen[i]),',',convert(rscale[i]),');')
  732.       else
  733.         writeln(outfile,'  ',rname[i],':=Getint(',convert(rlen[i]),');');
  734.     end;
  735.   end;
  736.   if subrfl = true then
  737.     writeln(outfile,'End;');
  738.   writeln(outfile,'{ End of Turbodraw Code }');
  739.   writeln(outfile);
  740.   close(outfile);
  741. end.
  742.